package com.example.SimpleCalculator;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //解果
    TextView result;
    //过程
    TextView process;
    //上次输入
    private String lastInput;
    //输入过程
    private StringBuilder processStringBuilder= new StringBuilder("");

    private String resultString="";

    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=findViewById(R.id.image4);

        //文本框
        result = findViewById(R.id.result);
        process = findViewById(R.id.process);
        //按钮
        findViewById(R.id.CE).setOnClickListener(this);
        findViewById(R.id.C).setOnClickListener(this);
        findViewById(R.id.dot).setOnClickListener(this);
        findViewById(R.id.reciprocal).setOnClickListener(this);
        findViewById(R.id.Prescription).setOnClickListener(this);
        findViewById(R.id.equal).setOnClickListener(this);
        findViewById(R.id.divide).setOnClickListener(this);
        findViewById(R.id.product).setOnClickListener(this);
        findViewById(R.id.subtract).setOnClickListener(this);
        findViewById(R.id.add).setOnClickListener(this);
        findViewById(R.id.zero).setOnClickListener(this);
        findViewById(R.id.nine).setOnClickListener(this);
        findViewById(R.id.eight).setOnClickListener(this);
        findViewById(R.id.seven).setOnClickListener(this);
        findViewById(R.id.six).setOnClickListener(this);
        findViewById(R.id.five).setOnClickListener(this);
        findViewById(R.id.four).setOnClickListener(this);
        findViewById(R.id.Three).setOnClickListener(this);
        findViewById(R.id.two).setOnClickListener(this);
        findViewById(R.id.one).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //清理
            case R.id.CE:
                clean();
                break;
                //删除
            case R.id.C:
                processStringBuilder.deleteCharAt(processStringBuilder.length()-1);
                break;
                // .
            case R.id.dot:
                processStringBuilder.append(".");
                break;
                //添加
            case R.id.add:
                processStringBuilder.append("+");

                break;
                //减法
            case R.id.subtract:
                processStringBuilder.append("-");

                break;
                //乘法
            case R.id.product:
                processStringBuilder.append("×");

                break;
                //除法
            case R.id.divide:
                processStringBuilder.append("➗");
                break;
                //开根号
            case R.id.Prescription:

                break;
                //倒数
            case R.id.reciprocal:

                break;
                //等号
            case R.id.equal:
                processStringBuilder.append("=");
                operation();
                break;
                //其他数字
            default:
                Button button = findViewById(v.getId());
                processStringBuilder.append(button.getText().toString());
                break;
        }
        //刷新
        refresh();
    }
    //运算
    private void operation() {
        imageView.setVisibility(View.VISIBLE);
        processStringBuilder=new StringBuilder("臭皮蛋,点什么点,我算不来,,,,");
        refresh();
    }

    private void refresh() {
        result.setText(resultString);
        process.setText(processStringBuilder);
    }

    private void clean() {
        resultString="";
        processStringBuilder= new StringBuilder("");

        result.setText(resultString);
        process.setText(processStringBuilder);
        imageView.setVisibility(View.GONE);
    }
}
